﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;
using Newtonsoft.Json.Linq;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    /// <summary>
    /// 省市区选择器,可用于设置异步模式,或同步模式
    /// </summary>
    public class PageForm_CitySelector:PageForm_FieldBase
    {
        private string _pickerTitle = "地区选择";
        private bool _disableDistrict = false;
        private string _currentValue = "";
        private string _placeHolder = "";
        private string _cityCodeName = "";
        private string _provinceCodeName = "";
        private string _districtCodeName = "";
        private bool _isrequired;

        static PageForm_CitySelector()
        {
            //var cityJsonArray = JArray.Parse(Encoding.UTF8.GetString(global::Kugar.MVC.MobileUI.WeUI.Properties.Resource1.city));

            

            //System.Reflection.Assembly app = System.Reflection.Assembly.GetAssembly(typeof(PageForm_CitySelector));

            //var stream = app.GetManifestResourceStream("city.json");
            //var data = stream.ReadAllBytes();

        }

        public PageForm_CitySelector(ViewContext viewContext) : base(viewContext)
        {
            viewContext.ViewBag.CityPicker = true;

            this.Css("mobileui-form-field weui-cells_city-picker");
        }

        /// <summary>
        /// 用于保存所选择的城市的编码字段名称,默认为 {Name}_CityCode
        /// </summary>
        /// <returns></returns>
        public PageForm_CitySelector CityCodeName(string cityCodeName)
        {
            _cityCodeName = cityCodeName;

            return this;
        }

        public PageForm_CitySelector ProvinceCodeName(string provinceCodeName)
        {
            _provinceCodeName = provinceCodeName;

            return this;
        }

        public PageForm_CitySelector DistrictCodeName(string districtCodeName)
        {
            _districtCodeName = districtCodeName;

            return this;
        }

        public PageForm_CitySelector PickerTItle(string title)
        {
            _pickerTitle = title;

            return this;
        }

        /// <summary>
        /// 禁止选择区,只选择省市,默认为false
        /// </summary>
        /// <param name="disable"></param>
        /// <returns></returns>
        public PageForm_CitySelector DisableDistrict(bool disable = true)
        {
            _disableDistrict = disable;

            return this;
        }

        public PageForm_CitySelector Placeholder(string text)
        {
            _placeHolder = text;

            return this;
        }

        public PageForm_CitySelector Required(bool isRequired = true)
        {
            _isrequired = isRequired;

            return this;
        }

        /// <summary>
        /// 当前选中的值,格式为  xx省xx市xx区
        /// </summary>
        /// <param name="currentValue"></param>
        /// <returns></returns>
        public PageForm_CitySelector Value(string currentValue)
        {
            _currentValue = currentValue;

            return this;
        }

        protected override void BeginRender()
        {
            base.BeginRender();

            this.Data("data-disabledistrict", _disableDistrict.ToStringEx());
            this.Data("data-pickertitle", _pickerTitle.ToStringEx());

        }

        protected override void RenderBody()
        {
            _provinceCodeName = _provinceCodeName.IfEmptyOrWhileSpace($"{getName()}_ProvinceCode");
            _cityCodeName= _cityCodeName.IfEmptyOrWhileSpace($"{getName()}_CityCode");
            _districtCodeName= _districtCodeName.IfEmptyOrWhileSpace($"{getName()}_DistrictCode");

            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" name=\"{getName()}\" value=\"{_currentValue}\" type=\"text\" {_isrequired.If("required")} readonly=\"readonly\" placeholder=\"{_placeHolder}\">");
            ViewContext.Writer.WriteLine($"<input class=\"provincecode\" name=\"{_provinceCodeName}\"  type=\"hidden\" />");
            ViewContext.Writer.WriteLine($"<input class=\"citycode\" name=\"{_cityCodeName}\"  type=\"hidden\" />");
            ViewContext.Writer.WriteLine($"<input class=\"districtcode\" name=\"{_districtCodeName}\"  type=\"hidden\" />");
        }
    }
}